package com.small.helper;

import com.small.anontation.Controller;
import com.small.anontation.Service;
import com.small.utils.ClassUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * 类操作助手
 * Created by fangzheng on 2018/1/30.
 */
public class ClassHelper {
    //存放所有加载的类
    private static Set<Class<?>> CLASS_SET = null;

    public ClassHelper() {
    }

    static {
        CLASS_SET = new HashSet<>();
        String basePackage = ConfigHelper.getAppBasePackage();
        CLASS_SET.addAll(ClassUtils.getClassSet(basePackage));
    }

    /**
     * 获取应用包下所有类
     *
     * @return
     */
    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    /**
     * 获取所有带@Service的类
     *
     * @return
     */
    public static Set<Class<?>> getServiceClassSet() {
        Set<Class<?>> services = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Service.class)) {
                services.add(cls);
            }
        }
        return services;
    }

    /**
     * 获取所有带@Controller的类
     *
     * @return
     */
    public static Set<Class<?>> getControllerClassSet() {
        Set<Class<?>> controllers = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(Controller.class)) {
                controllers.add(cls);
            }
        }
        return controllers;
    }

    /**
     * 获取应用包下所有bean类（service和controller）
     *
     * @return
     */
    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> beans = new HashSet<>();
        beans.addAll(getServiceClassSet());
        beans.addAll(getControllerClassSet());
        return beans;
    }


}
